package com.it.mongo.controller;

import com.it.mongo.model.dto.BookDTO;
import com.it.mongo.model.dto.GridfsDAO;
import com.it.mongo.service.BookMongoService;
import com.wordnik.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@RestController
public class BookMongoController {

    private static final Logger logger = LoggerFactory.getLogger(BookMongoController.class);

    @Autowired
    private BookMongoService bookMongoService;

    @Autowired
    private GridfsDAO gridfsDAO;


    @PutMapping("/addbook")
    @ApiOperation("新增")
    public Boolean inserBookInfo(@RequestBody BookDTO bookDTO) {
        return bookMongoService.inserBookInfo(bookDTO);
    }

    /**
     * 通用上传单个
     * 文件请求
     */
    @PostMapping("/uploadSingleFile")
    @ApiOperation(value = "上传单个文件")
    public Map uploadSingleFile(@RequestParam MultipartFile file, @RequestParam BookDTO bookDTO) throws IOException {
        logger.info("bookDTO:{}", bookDTO);
        String fileName = file.getOriginalFilename() + file.getSize();
        logger.info("fileName:{}", fileName);
        String bucket = String.valueOf(file.getSize());
        logger.info("bucket:{}", bucket);
        gridfsDAO.insertFile(file.getInputStream(), fileName, bucket);

        Map map = new HashMap<>();
        map.put("code", "200");
        map.put("result", "上传成功");
        return map;
    }

    /**
     * ---测试@RequestParam和@RequestPart的区别---
     *
     * @RequestPart 可以把json转成二进制流和文件流一起发送，以及JSON和JavaBean的映射
     * @RequestPart 解决同时上传文件和json的解决方案
     * <p>
     * 之前公司有一个上传功能是带有业务逻辑的，除了文件之外还有一个json信息的请求，
     * 当时是把json信息当成字符串用param方式传递给后台，后台再手动序列化一直觉得不太优雅，
     * 专研许久使用@RequestPart 解决了问题但是其中前后端都有一些需要注意的地方
     */
    @PostMapping("/uploadSingleFile2")
    @ApiOperation(value = "上传单个文件")
    public Map uploadSingleFile2(@RequestPart("file") MultipartFile file, @RequestPart("bookDTO") BookDTO bookDTO) throws IOException {

        logger.info("bookDTO:{}", bookDTO);
        System.out.println(bookDTO);

        String fileName = file.getOriginalFilename() + file.getSize();
        logger.info("fileName:{}", fileName);
        String bucket = String.valueOf(file.getSize());
        logger.info("bucket:{}", bucket);
        gridfsDAO.insertFile(file.getInputStream(), fileName, bucket);

        Map map = new HashMap<>();
        map.put("code", "200");
        map.put("result", "上传成功");
        return map;
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除文件")
    public Map uploadSingleFile2(@RequestParam String fileName, @RequestParam String bucket) {

        logger.info("fileName:{}, bucket:{}", fileName, bucket);
        gridfsDAO.deleteFile(fileName, bucket);

        Map map = new HashMap<>();
        map.put("code", "200");
        map.put("result", "删除成功");
        return map;
    }

}
